<template>
    <view class="history-container">
        <!-- 自定义顶部栏 -->
        <view class="custom-header" :style="{paddingTop: statusBarHeight + 'px'}">
            <view class="left-area" @tap="goBack">
                <image src="/static/images/back.png" mode="aspectFit" class="icon-back"></image>
            </view>
            <view class="center-area">
                <text class="title">观看历史</text>
            </view>
            <view class="right-area"></view>
        </view>
        
        <!-- 历史记录列表 -->
        <scroll-view scroll-y class="movie-list" v-if="watchedMovie.length">
            <view class="movie-item" v-for="(item, index) in watchedMovie" :key="index" @tap="goToMovieDetail(item.movie_id)">
                <!-- 海报 -->
                <view class="poster-wrapper">
                    <image :src="getPoster(item.poster)" mode="aspectFill" class="poster-image"></image>
                </view>
                
                <!-- 电影信息 -->
                <view class="info-wrapper">
                    <text class="movie-name">{{item.name}}</text>
                    <view class="user-rating">
                        <text class="rating-label">我评：</text>
                        <!-- Uni-app Rate 评分组件 -->
                        <uni-rate :value="item.user_score" readonly allow-half color="#f7ba2a" />
                    </view>
                    <text class="user-comment ellipsis">{{item.content}}</text>
                </view>
            </view>
        </scroll-view>
        
        <!-- 空状态 -->
        <view class="empty-state" v-else>
            <image src="/static/images/empty.png" mode="aspectFit" class="empty-image"></image>
            <text class="empty-text">暂无观看历史</text>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            watchedMovie: [],
            statusBarHeight: 20 // 默认状态栏高度
        }
    },
    onLoad(options) {
        // 获取系统状态栏高度
        const systemInfo = uni.getSystemInfoSync();
        this.statusBarHeight = systemInfo.statusBarHeight;
        
        // 假设从上个页面传入了UID
        if (options.UID) {
            this.loadHistory(options.UID);
        }
    },
    methods: {
        goBack() {
            uni.navigateBack();
        },
        loadHistory(userId) {
            uni.showLoading({
                title: '加载中...'
            });
            
            uni.request({
                url: 'http://localhost:9090/getWatchedMovie', // API地址
                method: 'GET',
                data: {
                    userId: userId
                },
                success: (res) => {
                    if (res.data.code === 1) {
                        this.watchedMovie = res.data.info;
                        // 按评论日期降序排序
                        this.watchedMovie.sort((a, b) => {
                            return new Date(b.comment_date) - new Date(a.comment_date);
                        });
                    } 
                },
                fail: (err) => {
                    console.error(err);
                    uni.showToast({
                        title: '网络请求失败',
                        icon: 'none'
                    });
                },
                complete: () => {
                    uni.hideLoading();
                }
            });
        },
        getPoster(path) {
            // 拼接完整的图片路径
            return 'http://localhost:9090' + path;
        },
        goToMovieDetail(movieId) {
            uni.navigateTo({
                url: `/pages/movie-detail/movie-detail?movie_id=${movieId}`
            });
        }
    }
}
</script>

<style>
.history-container {
    display: flex;
    flex-direction: column;
    width: 100%;
    min-height: 100vh;
    background-color: #f1f1f1;
}

/* 自定义顶部栏 */
.custom-header {
    width: 100%;
    height: 90rpx;
    background-color: #dd2727;
    display: flex;
    align-items: center;
    position: relative;
    z-index: 100;
}
.left-area, .right-area {
    width: 100rpx;
    height: 90rpx;
    display: flex;
    align-items: center;
    justify-content: center;
}
.icon-back {
    width: 40rpx;
    height: 40rpx;
}
.center-area {
    flex: 1;
    text-align: center;
}
.title {
    color: #ffffff;
    font-size: 36rpx;
    font-weight: 500;
}

/* 电影列表 */
.movie-list {
    flex: 1;
    padding: 20rpx;
    box-sizing: border-box;
}
.movie-item {
    display: flex;
    background-color: #fff;
    border-radius: 12rpx;
    padding: 20rpx;
    margin-bottom: 20rpx;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
}

/* 海报 */
.poster-wrapper {
    width: 160rpx;
    height: 220rpx;
    margin-right: 24rpx;
    border-radius: 8rpx;
    overflow: hidden;
    flex-shrink: 0;
}
.poster-image {
    width: 100%;
    height: 100%;
}

/* 信息 */
.info-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    overflow: hidden; /* 防止内容溢出 */
}
.movie-name {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
}
.user-rating {
    display: flex;
    align-items: center;
}
.rating-label {
    font-size: 26rpx;
    color: #ffb400;
    margin-right: 8rpx;
}
.user-comment {
    font-size: 26rpx;
    color: #666;
}
.ellipsis {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* 空状态 */
.empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding-top: 100rpx;
}
.empty-image {
    width: 200rpx;
    height: 200rpx;
    margin-bottom: 20rpx;
}
.empty-text {
    color: #999;
    font-size: 28rpx;
}
</style>